---
title: Function graph in console
description: Let's write an algorithm to output a graph of a function or a system of equations to the console in the form of text. We will use Java tools. For calculations.
sections: [Geometric figures,Text image]
tags: [java,math,fdlibm,console,rhombus,square,circumference,circle,text,image]
canonical_url: /en/2023/02/06/function-graph-in-console.html
url_translated: /ru/2023/02/05/function-graph-in-console.html
title_translated: График функции в консоли
date: 2023.02.06
lang: en
---

Let's write an algorithm to output a graph of a function or a system of equations to the console
in the form of text. We will use Java tools. For calculations, we will use the `Math` class, and
to bypass the range of coordinates, we will use two nested `for` loops. We draw in the console a
graph of a circle and graphs of a rhombus and a square inscribed in it.

Graph of a function with filling: [Drawing heart in console]({{ '/en/2023/03/08/drawing-heart-in-console.html#heart-shaped-graph' | relative_url }}).

{% include heading.html text="Equations of functions" hash="equations-of-functions" %}

We will check each point `(x,y)` from the output range of coordinates for belonging to function
graphs and output in accordance with this. We define the parameters of the functions and the
output range in advance before the start of bypass.

*Rhombus*.

{% include image_svg.html src="/img/rhombus-equation.svg"
style="width: 129.297pt; height: 14.944pt;"
alt="|x-a|+|y-b|=r." %}

*Square*.

{% include image_svg.html src="/img/square-equations.svg"
style="width: 167.599pt; height: 62.7502pt;"
alt="\begin{cases}|x-a|=c\land|y-b|\leqslant c,\\|y-b|=c\land|x-a|\leqslant c,\\c=r\times1/\sqrt2.\end{cases}" %}

*Circle*.

{% include image_svg.html src="/img/circumference-equation.svg"
style="width: 162.53pt; height: 17.9328pt;"
alt="\sqrt{(x-a)^2+(y-b)^2}=r." %}

*Parameters*.

`r` — circle radius;

`(a,b)` — figure center;

`c` — half a side of a square.

{% include heading.html text="Mathematical operations" hash="mathematical-operations" %}

To perform *basic* mathematical operations, Java uses the `FdLibm` library — *Freely Distributable
Math Library*. Most of the methods are implemented at the platform level to increase performance.
We will refer to them through the `Math` class.

For floating point calculations and rounding of results, we will use the methods of the `Math` class.

`abs(a)` — absolute value of the argument `a`;

`pow(a,b)` — raising the argument `a` to the power of the argument `b`;

`sqrt(a)` — square root of the argument `a`;

`ceil(a)` — rounding up the argument `a`;

`floor(a)` — rounding down the argument `a`.

{% include heading.html text="Algorithm description" hash="algorithm-description" %}

We take a range of coordinates on the plane in such a way that the displayed figure completely fits
in the printing area. We bypass the selected range with two nested `for` loops: first along the `y`
axis and then along the `x` axis. We check each point for belonging to the graphs of functions and
output. For clarity, we also output the coordinate axes, the origin of coordinates and the center
of the figure.

```java
// radius, figure center, offset
int r=12, a=5, b=-1, gap=2;
// boundaries of the text image
int xMin=a-r-gap, xMax=a+r+gap;
int yMin=b-r-gap, yMax=b+r+gap;
// half a side of an inscribed square
double c = Math.ceil(r/Math.sqrt(2));
// output the title
System.out.println("Radius: "+r+"; center: 0("+a+","+b+").");
// output to the console line by line from left to right from top to bottom
for (int y = yMax; y >= yMin; y--) {
    for (int x = xMin; x <= xMax; x++) {
        // check each point for belonging to the graphs and output
        if (Math.abs(x-a) + Math.abs(y-b) == r)
            System.out.print("z "); // rhombus
        else if (Math.abs(x-a) == c && Math.abs(y-b) <= c
                || Math.abs(y-b) == c && Math.abs(x-a) <= c)
            System.out.print("* "); // square
        else if (Math.floor(Math.sqrt(Math.pow(x-a,2)+Math.pow(y-b,2))) == r)
            System.out.print("o "); // circle
        else if (y == b && x == a)
            System.out.print("0 "); // figure center
        else if (y == 0 && x == 0)
            System.out.print("+-"); // origin of coordinates
        else if (y == 0) // abscissa axis (x)
            System.out.print(x == xMax ? ">x" : "--");
        else if (x == 0) // ordinate axis (y)
            System.out.print(y == yMax ? "↑y" : "¦ ");
        else // empty place
            System.out.print("  ");
    } // transition to a new line
    System.out.println();
}
```

Text image in console.

```
Radius: 12; center: 0(5,-1).
                  ↑y                                      
                  ¦                                       
                  ¦ o o o o z o o o o                     
                o o       z   z       o o                 
            o o   ¦     z       z         o o             
          * * * * * * z * * * * * z * * * * * *           
        o *       ¦ z               z         * o         
        o *       z                   z       * o         
      o   *     z ¦                     z     *   o       
      o   *   z   ¦                       z   *   o       
    o     * z     ¦                         z *     o     
    o     z       ¦                           z     o     
    o   z *       ¦                           * z   o     
----o z --* ------+---------------------------* --z o -->x
    z     *       ¦         0                 *     z     
    o z   *       ¦                           *   z o     
    o   z *       ¦                           * z   o     
    o     z       ¦                           z     o     
    o     * z     ¦                         z *     o     
      o   *   z   ¦                       z   *   o       
      o   *     z ¦                     z     *   o       
        o *       z                   z       * o         
        o *       ¦ z               z         * o         
          * * * * * * z * * * * * z * * * * * *           
            o o   ¦     z       z         o o             
                o o       z   z       o o                 
                  ¦ o o o o z o o o o                     
                  ¦                                       
                  ¦                                       
```
